﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KoPortal2;
using KoPortal2.Sys;
using KoPortal2.Sys.Data;
using KoPortal2.Sys.Data.Axapta;
using KoPortal2.Sys.Net;
using KoPortal2.Sys.Metadata;

namespace KoPortalWinFormsTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void btnEnvironmentTest_Click(object sender, EventArgs e)
        {
            //
            // KoEnvironment.GetSetting_CreateInDoc_LastSuccessfully
            //
            /*
            DateTime parDT = new DateTime();
            Exception actual;
            actual = KoEnvironment.GetSetting_CreateInDoc_LastSuccessfully(ref parDT);
            if (actual != null)
            {
                MessageBox.Show(actual.Message, "Error");
            }
            else
            {
                string s = string.Format("Result - {0}", parDT.ToString());
                MessageBox.Show(s, "Result - GetSetting_CreateInDoc_LastSuccessfully - ");
            }
            */

            //
            // KoEnvironment.SetSetting_CreateInDoc_LastSuccessfully
            //
            //DateTime parDT = DateTime.Now;
            //Exception actual;
            //actual = KoEnvironment.SetSetting_CreateInDoc_LastSuccessfully(parDT);
            //if (actual != null)
            //{
            //    MessageBox.Show(actual.Message, "Error");
            //}
            //else
            //{
            //    MessageBox.Show("Result - SetSetting_CreateInDoc_LastSuccessfully - OK");
            //}

            //
            // DBSPLog.Insert
            //
            string parName = "Name";
            string parKey = "Test Key";
            string parSubKey = "Test SubKey";
            string parValue = "Test Value";
            string parMsg = "Msg";
            int parErrId = 1;
            int parWarningLevel = 1;
            int parRetCode = 1;
            int parCountInfo = 1;
            Exception actual;
            string str = KoPortal2.Sys.Net.NetInfo.MachineName;
            actual = DBSPLog.Insert(parName, parKey, parSubKey, parValue
                , parMsg, parErrId, parWarningLevel, parRetCode, parCountInfo);
        }
        private void btnDBReceiptJour_Test_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            DateTime dt = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0);
            dt = dt - new TimeSpan(24, 0, 0);
            DataTable tbl = null;
            Exception err = DBReceiptJour.GetDataByDataTime(ref tbl, dt);
            if (err != null)
            {
                MessageBox.Show(err.Message, "Error");
            }
            else
            {
                dgDBReceiptJour.DataSource = tbl;
            }
        }
        private void btnDBShops_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            DateTime dt = new DateTime(d.Year-1, d.Month, d.Day, 0, 0, 0);
            dt = dt - new TimeSpan(24, 0, 0);
            DataTable tbl = null;
            Exception err = DBShops.GetDataByDataTime(ref tbl, dt);
            if (err != null)
            {
                MessageBox.Show(err.Message, "Error");
            }
            else
            {
                dgDBShops.DataSource = tbl;
            }
        }
        private void btnSyncShop_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            DateTime dtFrom = new DateTime(d.Year-1, d.Month, d.Day);
            MetadataSyncShops.UpdateShops(dtFrom);
        }
        private void btnJobCreateInDocs_Click(object sender, EventArgs e)
        {
            JobCreateInDocs2.JobCreateInDocs obj =
                new JobCreateInDocs2.JobCreateInDocs();
            obj.Execute(new Guid(), @"http://localhost");
        }
        private void btnDBSuplier_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            DateTime dt = new DateTime(d.Year-1, d.Month, d.Day, 0, 0, 0);
            dt = dt - new TimeSpan(24, 0, 0);
            DataTable tbl = null;
            Exception err = DBSuplier.GetDataByDataTime(ref tbl, dt);
            if (err != null)
            {
                MessageBox.Show(err.Message, "Error");
            }
            else
            {
                dgDBSuplier.DataSource = tbl;
            }
        }
        private void btnMySQLTest_Click(object sender, EventArgs e)
        {
            string connStr = KoPortal2.Sys.Data.MySQL.DBKoLocal.GetConnStrLocal();
            string sql =
@"SELECT * FROM shops_sale WHERE (accept=1) ORDER BY name";
            DataTable tbl = new DataTable();
            try
            {
                using (MySql.Data.MySqlClient.MySqlConnection conn =
                    new MySql.Data.MySqlClient.MySqlConnection(connStr))
                {
                    conn.Open();
                    using (MySql.Data.MySqlClient.MySqlCommand cmd =
                        new MySql.Data.MySqlClient.MySqlCommand(sql, conn))
                    {
                        cmd.CommandTimeout = 8000;
                        cmd.CommandType = global::System.Data.CommandType.Text;
                        using (MySql.Data.MySqlClient.MySqlDataAdapter a =
                            new MySql.Data.MySqlClient.MySqlDataAdapter(cmd))
                        {
                            a.Fill(tbl);
                            dgMySQL_Test.DataSource = tbl;
                        }
                    }
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnSPUserSync_Click(object sender, EventArgs e)
        {
            JobUsersSync2.Features.JobUsersSync2.JobUsersSync2 target =
                new JobUsersSync2.Features.JobUsersSync2.JobUsersSync2();
            Guid contentDbId = new Guid();
            target.Execute(contentDbId);
        }

    }
}

